{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Build a graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "arrays = [tf.constant(np.arange(50*i, 50*(i + 1))) for i in range(100)]\n",
    "batch_size = 8\n",
    "dataset_size = 100*50\n",
    "\n",
    "\n",
    "dataset = tf.data.Dataset.from_tensor_slices(arrays)\n",
    "num_shards = len(arrays)\n",
    "\n",
    "\n",
    "# read the files in parallel\n",
    "dataset = dataset.shuffle(buffer_size=num_shards)\n",
    "dataset = dataset.apply(tf.contrib.data.parallel_interleave(\n",
    "    lambda x: tf.data.Dataset.from_tensor_slices(x), cycle_length=10\n",
    "))\n",
    "dataset = dataset.prefetch(buffer_size=batch_size)\n",
    "\n",
    "\n",
    "dataset = dataset.shuffle(buffer_size=125)\n",
    "dataset = dataset.repeat(3)\n",
    "\n",
    "\n",
    "dataset = dataset.apply(tf.contrib.data.map_and_batch(\n",
    "    lambda x: x, batch_size=batch_size,\n",
    "    num_parallel_batches=1, drop_remainder=False\n",
    "))\n",
    "dataset = dataset.prefetch(buffer_size=1)\n",
    "\n",
    "\n",
    "iterator = tf.data.Iterator.from_structure(dataset.output_types, dataset.output_shapes)\n",
    "init = iterator.make_initializer(dataset)\n",
    "numbers = iterator.get_next()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_epochs = 1\n",
    "result = []\n",
    "with tf.Session() as sess:\n",
    "    for i in range(num_epochs):\n",
    "        sess.run(init)\n",
    "        try:\n",
    "            while True:\n",
    "                output = sess.run(numbers)\n",
    "                result.extend(list(output))\n",
    "        except tf.errors.OutOfRangeError:\n",
    "            pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "_, counts = np.unique(np.array(result), return_counts=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.hist(result, bins=100);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.figure(figsize=(15, 7))\n",
    "plt.plot(result, 'r.');"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
